package com.zdkj.subway.check.controller.subwayRat.conductorDifferences.cheZhan;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zdkj.common.security.service.PigxUser;
import com.zdkj.common.security.util.SecurityUtils;
import com.zdkj.subway.check.controller.CommonController;
import com.zdkj.subway.check.entity.Page;
import com.zdkj.subway.check.entity.SwAuStDiffNotice;
import com.zdkj.subway.check.entity.SwAuStationShort;
import com.zdkj.subway.check.entity.SwAuStationTicketDifference;
import com.zdkj.subway.check.enums.ResultDetailMsg;
import com.zdkj.subway.check.mapper.SwAuStDiffNoticeMapper;
import com.zdkj.subway.check.mapper.SwAuStationShortMapper;
import com.zdkj.subway.check.mapper.SwAuStationTicketDifferenceMapper;
import com.zdkj.subway.check.result.Result;
import com.zdkj.subway.check.util.DateFormat;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.*;

/**
 * 车站补款确认通知
 */
@Slf4j
@RestController
@RequestMapping("/check")
public class SwAuStationShortController {
    @Resource
    SwAuStationShortMapper swAuStationShortMapper;
    @Resource
    SwAuStDiffNoticeMapper swAuStDiffNoticeMapper;
    @Resource
    SwAuStationTicketDifferenceMapper swAuStationTicketDifferenceMapper;
    @Autowired
    CommonController commonController;

    /**
     * 车站票款补款通知查询
     * @param page
     * @param swAuStationShort
     * @param param
     * @return
     */
     @RequestMapping("/getSwAuStationShortList")
     @ApiOperation(value = "车站票款补款通知查询", notes = "车站票款补款通知查询")
     public Page getSwAuStationShortList(Page page, SwAuStationShort swAuStationShort, @RequestParam Map<String,Object> param){
        try{
            List<Map<String,Object>> res=swAuStationShortMapper.getSwAuStationShortList(param);
            page.setData(res,true);
        }catch (Exception e){
            log.error("{}",e);
        }
        return page;
     }

    /**
     * 车站票款补款通知查询
     * @param page
     * @param param
     * @return
     */
    @RequestMapping("/getSwAuStationShortLimit")
    @ApiOperation(value = "车站票款补款通知查询", notes = "车站票款补款通知查询")
    public Page getSwAuStationShortLimit(Page page,@RequestParam Map<String,String> param){
        Map req = new HashMap();
        List<Integer> ids = new ArrayList<>();
        if(null != param.get("ids")){
            ids = (List)JSONObject.parseArray(String.valueOf(param.get("ids")));
        }else{
            return page;
        }
        List swAuStationShortList = new ArrayList();
        for(Integer id : ids){
            SwAuStationShort swAuStationShort = swAuStationShortMapper.selectById(id);
            swAuStationShortList.add(swAuStationShort);
        }
        page.setData(swAuStationShortList,true);
        return page;
    }

    /**
     * 设置补款期限
     * @param param
     * @return
     */
    @RequestMapping("/updateSwAuStationShortLimit")
    @ApiOperation(value = "设置补款期限", notes = "设置补款期限")
    public Result updateSwAuStationShortLimit(@RequestParam Map<String,String> param){
        if(null != param.get("list")){
            JSONArray list = JSONObject.parseArray(param.get("list"));
            if(null != list){
                List<SwAuStationShort> swAuStationShorts = JSONObject.parseArray(list.toJSONString(), SwAuStationShort.class);
                for(SwAuStationShort swAuStationShort :swAuStationShorts){
                    swAuStationShortMapper.updateById(swAuStationShort);
                }
            }
        }
        Result res=new Result(ResultDetailMsg.OK);
        return res;
    }

    /**
     * 设置入账时间
     * @param rows
     * @return
     */
    @RequestMapping("/setSwAuStationShortAccuntTime")
    @ApiOperation(value = "设置入账时间", notes = "设置入账时间")
    public Result setSwAuStationShortAccuntTime(String rows){
        Result res=new Result(ResultDetailMsg.OK);
        try{
            JSONArray js=JSONArray.parseArray(rows);
            for(int i=0;i<js.size();i++){
                SwAuStationShort swAuStationShort = js.getObject(i, SwAuStationShort.class);
                swAuStationShortMapper.updateById(swAuStationShort);
            }
        }catch (Exception e){
            log.error("{}",e);
            res.setMsg("设置入账时间失败！");
            res.setResult(ResultDetailMsg.CHECK_UPDATE_ERROR);
        }
        return res;
    }

    /**
     * 通知
     * @param param
     * @return
     */
    @RequestMapping("/noticeSwAuStationShort")
    @ApiOperation(value = "通知", notes = "通知")
    @Transactional(rollbackFor = Exception.class)
    public Result noticeSwAuStationShort(@RequestBody Map<String,Object> param){
        Result res=new Result(ResultDetailMsg.OK);
        PigxUser user = SecurityUtils.getUser();
        try{
            if(null != param.get("list")){
                List<Map<String,Object>> list = (List)JSONObject.parseArray(String.valueOf(param.get("list")));
                for(Map<String,Object> m : list){
                    String supPaymentLimit = String.valueOf(m.get("supPaymentLimit"));
                    Calendar calendar = new GregorianCalendar();
                    calendar.setTime(new Date());
                    calendar.add(calendar.DATE,Integer.valueOf(supPaymentLimit));
                    SwAuStationShort swAuStationShort = swAuStationShortMapper.selectById(Integer.parseInt(String.valueOf(m.get("id"))));
                    swAuStationShort.setNoticeState("1");//已通知
                    swAuStationShort.setNoticeTime(new Date());
                    swAuStationShort.setSupPaymentTimelimit(calendar.getTime());
                    swAuStationShort.setNoticeName(user.getNikeName());
                    swAuStationShortMapper.updateById(swAuStationShort);
                }
            }
            commonController.innerBatch(param);
        }catch (Exception e){
            log.error("{}",e);
            res.setResult(ResultDetailMsg.CHECK_UPDATE_ERROR);
            res.setMsg("通知失败");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return res;
    }

    /**
     * 车站补款确认通知 补款
     * @param jsonObject
     * @return
     */
    @RequestMapping("/buKuanSwAuStationShort")
    @ApiOperation(value = "车站补款确认通知 补款", notes = "车站补款确认通知 补款")
    public Result buKuanSwAuStationShort(@RequestBody JSONObject jsonObject){
        Result res=new Result(ResultDetailMsg.OK);
        try {
            SwAuStationShort swAuStationShort = JSONObject.parseObject(jsonObject.toJSONString(), SwAuStationShort.class);
            if(ObjectUtil.isNotEmpty(swAuStationShort)){
                //日期格式转换
                LocalDate supPaymentTimelimit = DateFormat.dateTOLocalDate(swAuStationShort.getSupPaymentTimelimit());
                LocalDate newLocalDate = DateFormat.dateTOLocalDate(new Date());
                if(supPaymentTimelimit.compareTo(newLocalDate)>=0){
                    swAuStationShort.setIsLimitSuppay("1");
                }else{
                    swAuStationShort.setIsLimitSuppay("0");
                }
                //根据relationid查询
                SwAuStDiffNotice swAuStDiffNotice = swAuStDiffNoticeMapper.selectById(swAuStationShort.getRelationid());
                if(ObjectUtil.isNotEmpty(swAuStDiffNotice)){
                    swAuStDiffNotice.setSuappStatus("1");
                    SwAuStationTicketDifference swAuStationTicketDifference =
                            swAuStationTicketDifferenceMapper.selectById(swAuStDiffNotice.getRelationid());
                    if(ObjectUtil.isNotEmpty(swAuStationTicketDifference)){
                        swAuStationTicketDifference.setShortAmount(swAuStationShort.getShortAmount());
                        //差异金额= 营收-现金缴款单-补款金额
                        swAuStationTicketDifference.setDiffAmount(swAuStationTicketDifference.getCashReceiptsAmount().subtract(swAuStationTicketDifference.getCashPayAmount()).subtract(swAuStationTicketDifference.getShortAmount()));
						//swAuStationTicketDifference.setDiffAmount(swAuStationTicketDifference.getCashPayAmount().subtract(swAuStationTicketDifference.getCashReceiptsAmount()).subtract(swAuStationTicketDifference.getShortAmount()));
                    }
                    swAuStDiffNotice.setSuappTime(new Date());
                    swAuStDiffNoticeMapper.updateById(swAuStDiffNotice);
                    swAuStationTicketDifferenceMapper.updateById(swAuStationTicketDifference);
                }
                swAuStationShort.setIsNotice(1);
                if(swAuStationShort.getExtraMoneyTime()!=null){
                    swAuStationShort.setAccountTime(swAuStationShort.getExtraMoneyTime());//入账日期
                }
                swAuStationShortMapper.updateById(swAuStationShort);
            }
        }catch (Exception e){
            log.error("{}",e);
            res.setResult(ResultDetailMsg.CHECK_UPDATE_ERROR);
            res.setMsg("补款失败");
        }
        return res;
    }


    /**
     * 车站补款确认通知 确认
     * @param ids
     * @return
     */
    @RequestMapping("/querenSwAuStationShort")
    @ApiOperation(value = "车站补款确认通知 确认", notes = "车站补款确认通知 确认")
    public Result querenSwAuStationShort(String ids){
        Result res=new Result(ResultDetailMsg.OK);
        try {
            //批量查询
            List<SwAuStationShort> swAuStationShorts = swAuStationShortMapper.selectBatchIds(Arrays.asList(ids.split(",")));
            for(SwAuStationShort swAuStationShort:swAuStationShorts){
                swAuStationShort.setConductorConfirmStatus("1");
                swAuStationShortMapper.updateById(swAuStationShort);
            }
        }catch (Exception e){
            log.error("{}",e);
            res.setResult(ResultDetailMsg.CHECK_UPDATE_ERROR);
            res.setMsg("确认失败");
        }
        return res;
    }

}
